import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

class Add{
    Connection connection;
    String s = "insert into student values (?,?)";
    PreparedStatement statement;

    Add(Connection c) throws SQLException {
        this.connection = c;
        statement = connection.prepareStatement(s);
    }

    int a(int id,String name) throws SQLException {
        statement.setInt(1,id);
        statement.setString(2,name);
        return statement.executeUpdate();
    }

    void free() throws SQLException {
        statement.close();
    }
}

class Delete{
    Connection connection;
    String s1 = "delete from student where id = ? ";
    String s2 = "delete from student where name = ? ";
    PreparedStatement statement;

    Delete(Connection c) throws SQLException {
        this.connection = c;
        statement = connection.prepareStatement(s1);

    }

    int del(int id) throws SQLException {
        PreparedStatement statement = connection.prepareStatement(s1);
        statement.setInt(1,id);
        return statement.executeUpdate();
    }

    int del(String name) throws SQLException {
        PreparedStatement statement = connection.prepareStatement(s2);
        statement.setString(1,name);
        return statement.executeUpdate();
    }

    void free() throws SQLException {
        statement.close();
    }
}

class Update{
    Connection connection;
    String s1 = "update student set name = ? where id = ?";
    String s2 = "update student set id = ? where name = ?";
    PreparedStatement statement;

    Update(Connection c) throws SQLException {
        this.connection = c;
        statement = connection.prepareStatement(s1);
    }

    int up(String name, int id) throws SQLException {
        statement = connection.prepareStatement(s1);
        statement.setString(1,name);
        statement.setInt(2,id);
        return statement.executeUpdate();
    }

    int up(int id,String name) throws SQLException {
        statement = connection.prepareStatement(s2);
        statement.setInt(1,id);
        statement.setString(2,name);
        return statement.executeUpdate();
    }

    void free() throws SQLException {
        statement.close();
    }
}

class Search{
    Connection connection;
    String s = "select * from student";
    String s1 = "select * from student where id = ?";
    String s2 = "select * from student where name = ?";
    PreparedStatement statement;

    Search(Connection c) throws SQLException {
        this.connection = c;
        statement = connection.prepareStatement(s);
    }

    ResultSet search(String name) throws SQLException {
        statement = connection.prepareStatement(s2);
        statement.setString(1,name);
        return statement.executeQuery();
    }

    ResultSet search(int id) throws SQLException {
        statement = connection.prepareStatement(s1);
        statement.setInt(1,id);
        return statement.executeQuery();
    }

    ResultSet search() throws SQLException {
        statement = connection.prepareStatement(s);
        return statement.executeQuery();
    }

    void free() throws SQLException {
        statement.close();
    }
}

public class Demo2 {
    public static void main(String[] args) {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/practice?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("0000");

        Connection connection = null;

        try {
            connection = dataSource.getConnection();

            //增
            Add add = new Add(connection);
            System.out.println(add.a(1, "张三"));
            System.out.println(add.a(2, "李四"));
            System.out.println(add.a(4, "王五"));

            add.free();

            //改
            Update update = new Update(connection);
            System.out.println(update.up(3, "李四"));
            update.free();

            //删
            Delete delete = new Delete(connection);
            System.out.println(delete.del(1));
            delete.free();

            //查
            Search search = new Search(connection);
            ResultSet resultSet = search.search();
            while (resultSet.next()){
                System.out.println("id="+resultSet.getInt("id")+" name="+resultSet.getString("name"));
            }
            connection.close();

        } catch (SQLException e) {
            e.printStackTrace();

        }
    }
}
